<?php
/**
 * Move selected messages to Delete Box
 * 
 * @author Xavier VILAIN
 * @package Orx\Mailer
 * @license 
 */

use Orx\Mailer\MailBox\Message;
$xview = Xview::instance();
$messageids = $xview->val('messageids');
if(!empty($messageids)) {
	// Load All Messages
	$nbNessages = 0;
	$messageids = explode(':',$messageids);
	foreach($messageids as $messageid) {
		if(empty($messageid)) {
			continue;
		}
		try {
			$message = \Fox\Dao::load(Message::className(),$messageid);
			$message->status = Message::STATUS_DELETED;
			$message->prepareUpdate(Message::CHANGE_DELETE_BOX);
			$message->update();
			$nbNessages++;
		} catch(Fox\Object\Exception\NotExist $e) {
			continue;
		} catch(Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		}
	}
	if($nbNessages > 0) {
		Fox\Message::Success(Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::X_MESSAGES_HAS_BEEN_MOVED_TO_BASKET,$nbNessages));
	} elseif(!Fox\Message::errors()) {
		Fox\Message::Success(Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::X_MESSAGES_HAS_BEEN_MOVED_TO_BASKET,'0'));
	}
	echo $xview->messages(true,true);
}